package com.mf.cloud.exp;

import com.mf.cloud.resp.ResultData;
import com.mf.cloud.resp.ReturnCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * ClassName: RestControllerAdvice
 * Package: com.mf.cloud.exp
 *
 * @Author: lmf
 * @Create: 2024/3/6 22:06
 * @Version: 1.0
 * Description:  全局异常处理
 */
@Slf4j
// 注解将作用在所有注解了@RequestMapping的控制器的方法上。
@org.springframework.web.bind.annotation.RestControllerAdvice
public class RestControllerAdvice {
    @ExceptionHandler(RuntimeException.class) // 捕获运行时异常错误
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) // 捕获http的状态码
    public ResultData<String> exception(Exception e) {
        // 将异常信息记录到日志中
        log.error("全局异常信息:{}", e.getMessage(), e);
        return ResultData.fail(ReturnCodeEnum.RC500.getCode(), e.getMessage());
    }
}
